package com.universal_library.fragment;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.LinearLayout;

import com.universal_library.R;
import com.universal_library.adapter.BaseViewPagerAdapter;

import java.lang.reflect.Field;

import static com.universal_library.utils.DeviceUtil.getDisplayMetrics;


/**
 * Created by solin on 2017/9/12.
 */

public abstract class BaseViewPageFragment extends BaseFragment {
    protected TabLayout mTabNav;
    protected ViewPager mBaseViewpager;
    public BaseViewPagerAdapter mViewpageAdapter;
    public Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 1:
                    //修改第一标题

                    break;
                case 2:
                    //修改第二个标题
                    break;
                case 3:
                    //修改第三个标题
                    break;
            }
        }
    };

    @Override
    protected void initData() {

    }

    @Override
    protected void initView(View mRoot) {
        mTabNav = (TabLayout) mRoot.findViewById(R.id.tab_nav);
        mBaseViewpager = (ViewPager) mRoot.findViewById(R.id.base_viewPager);

        mViewpageAdapter = new BaseViewPagerAdapter(getChildFragmentManager(), getPagers(),getContext());
        mBaseViewpager.setAdapter(mViewpageAdapter);
        mTabNav.setupWithViewPager(mBaseViewpager);
        mBaseViewpager.setCurrentItem(0, true);

//        setIndicator(getContext(), mTabNav, 50, 50);
    }

    public abstract BaseViewPagerAdapter.PagerInfo[] getPagers();

    @Override
    protected int getLayoutId() {
        return getLayout();
    }

    protected abstract int getLayout();




    /**
     * 修改 tablayout indicator 样式
     *
     * @param context
     * @param tabs
     * @param leftDip
     * @param rightDip
     */
    public static void setIndicator(Context context, TabLayout tabs, int leftDip, int rightDip) {
        Class<?> tabLayout = tabs.getClass();
        Field tabStrip = null;
        try {
            tabStrip = tabLayout.getDeclaredField("mTabStrip");
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        }

        tabStrip.setAccessible(true);
        LinearLayout ll_tab = null;
        try {
            ll_tab = (LinearLayout) tabStrip.get(tabs);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }

        int left = (int) (getDisplayMetrics(context).density * leftDip);
        int right = (int) (getDisplayMetrics(context).density * rightDip);

        for (int i = 0; i < ll_tab.getChildCount(); i++) {
            View child = ll_tab.getChildAt(i);
            child.setPadding(0, 0, 0, 0);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.MATCH_PARENT, 1);
            params.leftMargin = left;
            params.rightMargin = right;
            child.setLayoutParams(params);
            child.invalidate();
        }
    }

}
